<?php
/**
 * @title 1838. 最高频元素的频数 - 排序+双循环
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Integer
     */
    function maxFrequency($nums, $k) {
        /**
         * @since 2021-07-20 排序
         */
        sort($nums);

        /**
         * @since 2021-07-20 最大频数
         */
        $max = 0;

        /**
         * @since 2021-07-20 上个数
         */
        $lastNum = null;

        /**
         * @since 2021-07-20 迭代查找
         */
        $len = count($nums);
        for($i=$len-1; $i>0; $i--){
            /**
             * @since 2021-07-20 数字
             */
            $num = $nums[$i];
            if($lastNum === $num){
                continue;
            } else {
                $lastNum = $num;
            }

            /**
             * @since 2021-07-20 频数, k
             */
            $m = 1;
            $mK = $k;

            /**
             * @since 2021-07-20 求频数
             */
            for ($j=$i-1; $j>=0; $j--){
                $subNum = $num-$nums[$j];
                if($subNum <= $mK){
                    $mK -= $subNum;
                    $m++;
                } else {
                    break;
                }
            }

            /**
             * @since 2021-07-20 更新频数
             */
            // echo "[", $j+1, ", {$i}] = {$m}", PHP_EOL;
            $max = max($max, $m);
            if($max >= $i){
                break;
            } else {}
        }

        /**
         * @return
         */
        return $max;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202107/2021.07.19.php
 */
$datas = [
    // [[1,2,4], 5], // 3
    // [[1,4,8,13], 5], // 2
    // [[3,9,6], 2], // 1
    [[9930,9923,9983,9997,9934,9952,9945,9914,9985,9982,9970,9932,9985,9902,9975,9990,9922,9990,9994,9937,9996,9964,9943,9963,9911,9925,9935,9945,9933,9916,9930,9938,10000,9916,9911,9959,9957,9907,9913,9916,9993,9930,9975,9924,9988,9923,9910,9925,9977,9981,9927,9930,9927,9925,9923,9904,9928,9928,9986,9903,9985,9954,9938,9911,9952,9974,9926,9920,9972,9983,9973,9917,9995,9973,9977,9947,9936,9975,9954,9932,9964,9972,9935,9946,9966],3056], // 73
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->maxFrequency(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
